import dayjs from 'dayjs';

export const formatTime = (time) => {
	if (!time) return '';

	const now = dayjs();
	const target = dayjs(time);
	// 计算时间差
	const diffSeconds = now.diff(target, 'second');
	const diffMinutes = now.diff(target, 'minute');
	const diffHours = now.diff(target, 'hour');
	const diffDays = now.diff(target, 'day');

	// 当天处理
	if (now.isSame(target, 'day')) {
		if (diffMinutes < 1) return '刚刚';
		if (diffHours < 1) return `${diffMinutes}分钟前`;
		return `${diffHours}小时前`;
	}

	// 昨天处理
	if (diffDays === 1) return '昨天';

	// 5天内处理
	if (diffDays <= 5) return `${diffDays}天前`;

	// 同年处理
	if (now.year() === target.year()) return target.format('MM-DD');

	// 其他情况
	return target.format('YYYY-MM-DD');
}